package cn.edu.cqupt.cxcy.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.cqupt.cxcy.dao.AdminDao;
import cn.edu.cqupt.cxcy.dto.AdminImpl;
import cn.edu.cqupt.cxcy.dto.IndexImpl;
import cn.edu.cqupt.cxcy.model.Field;
import cn.edu.cqupt.cxcy.model.Fieldtype;
import cn.edu.cqupt.cxcy.model.Noticetype;
import cn.edu.cqupt.cxcy.util.Tools;
import cn.edu.cqupt.cxcy.util.Validate;


/**
 * 主页-【场地预约】
 * @author Dancer-mno
 * @date 2015年10月17日
 */
@SuppressWarnings("serial")
public class IndexNavCdyy extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String f_peoper   = req.getParameter("peoper"); // '申请人',
		System.out.println(f_peoper);
		if( f_peoper != null) {
			String f_tel      = req.getParameter("tel"); // '联系人的联系电话',
			String ft_id      = req.getParameter("ft_id"); // '场地类别',
			String f_stardate = req.getParameter("stardate"); // '开始使用时间',
			String f_startime = req.getParameter("startime"); // '开始使用的具体时间',
			String f_endtime  = req.getParameter("endtime"); // '结束使用的具体时间',
			String f_explain  = req.getParameter("explain"); // '申请人额外说明',
			HttpSession session = req.getSession();
			@SuppressWarnings("unchecked")
			List<Object> user= (List<Object>) session.getAttribute( "user");
			boolean flag = false;
			if(user != null) {
				Field field = new Field();
				field.setF_peoper(f_peoper);
				field.setF_tel(f_tel);
				field.setFt_id(Validate.stringIsNO(ft_id)?Integer.parseInt(ft_id):2);
				field.setF_stardate(Tools.getDateyyyyMMdd((f_stardate)));
				field.setF_user((String)user.get(4));
				field.setF_startime(Tools.getTimeddHHmm(f_startime));
				field.setF_endtime(Tools.getTimeddHHmm(f_endtime));
				field.setF_explain(f_explain);
				field.setF_user((String)user.get(4));
				flag = new IndexImpl().addCdyy(field);
			}
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			out.println(flag);
			out.flush();
			return;
		}
		String pjax_back  = "$(function(){$(\"a.pjax[href='index_cdyy']\").trigger('click');});";
		if(req.getHeader("x-pjax") == null || req.getHeader("x-pjax").equals("null")) {
			req.setAttribute("pjax_back", pjax_back);
			List<Noticetype> nt = new IndexImpl().noticeTypes();
			req.setAttribute("nt", nt);
			req.getRequestDispatcher("html/index.jsp").forward(req, resp);
			return;
		}  else {
			AdminDao admin = new AdminImpl();
			List<Fieldtype> field = admin.fieldType();
			req.setAttribute("field", field);
			req.getRequestDispatcher("html/cdyy.jsp").forward(req, resp);
			return;
		}
	}
}
